package leetcode;

import java.util.Stack;

/**
 * @author Liangyaoyun
 */
public class ReversePolishNotation {
    class Solution {
        // 栈实现   8 ms	36.7 MB
        public  int evalRPN(String[] tokens) {
            Stack<Integer> numStack = new Stack<>();
            Integer op1, op2;
            for (String s : tokens) {
                switch (s) {
                    case "+":
                        op2 = numStack.pop();
                        op1 = numStack.pop();
                        numStack.push(op1 + op2);
                        break;
                    case "-":
                        op2 = numStack.pop();
                        op1 = numStack.pop();
                        numStack.push(op1 - op2);
                        break;
                    case "*":
                        op2 = numStack.pop();
                        op1 = numStack.pop();
                        numStack.push(op1 * op2);
                        break;
                    case "/":
                        op2 = numStack.pop();
                        op1 = numStack.pop();
                        numStack.push(op1 / op2);
                        break;
                    default:
                        numStack.push(Integer.valueOf(s));
                        break;
                }
            }
            return numStack.pop();
        }

    }
}
